module SocketManager {
	export class socket {
		public gameType = null;
		private ip = null;
		private port = null;
		private socket: egret.WebSocket = null;
		private mainId: number = null;
		private subId: number = null;
		private byet: egret.ByteArray = null;
		private isShowWait: boolean = false;
		private heartCheckEnable: boolean = false;
		private timeout: number = null;
		public constructor(gameType, ip, port, byet = null, isShowWait = false, heartCheckEnable = false) {
			this.gameType = gameType;
			this.ip = ip;
			this.port = port;
			this.byet = byet;
			this.isShowWait = isShowWait;
			this.heartCheckEnable = heartCheckEnable;
			this.connectServer();
		}
		//连接服务器(此方法不允许外外部调用,发消息请用 sendMessage)
		private connectServer() {
			EffectUtils.showWaritPanel();
			this.timeout = egret.setTimeout(() => {
				EffectUtils.hideWaritPanel();
				egret.clearTimeout(this.timeout);
				this.timeout = null;
			}, this, 3000);
			if (this.socket) {
				this.socket.removeEventListener(egret.Event.CONNECT, this.onSocketOpen, this);
				this.socket.removeEventListener(egret.ProgressEvent.SOCKET_DATA, this.onReceiveMessage, this);
				this.socket.removeEventListener(egret.Event.CLOSE, this.onSocketClose, this);
				this.socket.removeEventListener(egret.IOErrorEvent.IO_ERROR, this.onIoError, this);
				this.socket.close();
				this.socket = null;
			}
			this.socket = new egret.WebSocket();
			this.socket.type = egret.WebSocket.TYPE_BINARY
			this.socket.addEventListener(egret.Event.CONNECT, this.onSocketOpen, this);
			this.socket.addEventListener(egret.ProgressEvent.SOCKET_DATA, this.onReceiveMessage, this);
			this.socket.connectByUrl("wss://" + this.ip + "/" + this.port);
		}
		//连接成功返回
		private onSocketOpen(e: egret.Event): void {
			EffectUtils.hideWaritPanel();
			if (this.timeout != null) {
				egret.clearTimeout(this.timeout);
				this.timeout = null;
			}
			this.socket.addEventListener(egret.Event.CLOSE, this.onSocketClose, this);
			this.socket.addEventListener(egret.IOErrorEvent.IO_ERROR, this.onIoError, this);
			if (Global.isNotEmpty(this.byet)) {
				this.sendMessage(this.byet, this.isShowWait);
				this.isShowWait = null;
			}
			if (this.heartCheckEnable) {
				this.heartCheck()
			}
		}
		//连接关闭
		private onSocketClose(e: egret.Event): void {
			SocketManager.onSocketClose(this);
		}
		private onIoError(e: egret.Event) {
			egret.log(this.gameType + "数据发送失败");
		}
		//向服务端发送消息
		public sendMessage(byet: egret.ByteArray, isShowWait: boolean = false): void {
			if (isShowWait) {
				EffectUtils.showWaritPanel();
				this.timeout = egret.setTimeout(() => {
					EffectUtils.hideWaritPanel();
					EffectUtils.textFadeAway("服务器未响应，请重试!")
					egret.clearTimeout(this.timeout);
					this.timeout = null;
				}, this, 3000);
			}
			if (this.socket.connected) {
				if (Global.isNotEmpty(byet)) {
					this.socket.writeBytes(byet, 0, byet.bytesAvailable);
					this.socket.flush();
				}
			} else {
				this.byet = byet;
				this.isShowWait = isShowWait;
				this.connectServer();
			}
		}
		//消息返回  
		private onReceiveMessage(): void {
			if (this.timeout != null) {
				egret.clearTimeout(this.timeout);
				this.timeout = null;
			}
			EffectUtils.hideWaritPanel();
			var magBytesAvailable = 0;
			var byte: egret.ByteArray = new egret.ByteArray();
			this.socket.readBytes(byte);
			let data = byte.buffer
			SocketManager.onReceiveMessage(this.gameType, data);
		}
		//关闭secket连接
		public closeSocket() {
			if (this.socket) {
				this.socket.removeEventListener(egret.Event.CONNECT, this.onSocketOpen, this);
				this.socket.removeEventListener(egret.ProgressEvent.SOCKET_DATA, this.onReceiveMessage, this);
				this.socket.removeEventListener(egret.Event.CLOSE, this.onSocketClose, this);
				this.socket.removeEventListener(egret.IOErrorEvent.IO_ERROR, this.onIoError, this);
				this.socket.close();
				this.socket = null;
			}
		}
		//心跳
		public heartCheck() {
			// egret.clearInterval(this.heartCheckTime)
			// this.heartCheckTime = egret.setInterval(function () {
			// 	SocketManager.sendMessage(this.gameType, RegDataGamePublic.heartCheck, {});
			// }, this, 5000)
		}
	}
}